package com.qys.livesMall.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qys.livesMall.system.model.dto.sys.PlatformAdvertisementDetailDto;
import com.qys.livesMall.system.model.entity.H2PlatformAdvertisementDetail;
import com.qys.livesMall.system.mapper.H2PlatformAdvertisementDetailMapper;
import com.qys.livesMall.system.model.vo.sys.PlatformAdvertisementDetailVO;
import com.qys.livesMall.system.service.H2PlatformAdvertisementDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 平台广告管理的轮播图 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2023-07-05
 */
@Service
public class H2PlatformAdvertisementDetailServiceImpl extends ServiceImpl<H2PlatformAdvertisementDetailMapper, H2PlatformAdvertisementDetail> implements H2PlatformAdvertisementDetailService {

    @Resource
    private  H2PlatformAdvertisementDetailMapper h2PlatformAdvertisementDetailMapper;
    @Override
    public void add(List<PlatformAdvertisementDetailDto> platformAdvertisementDetailDtos, Long id) {
        if(CollectionUtils.isNotEmpty(platformAdvertisementDetailDtos)){
           List<H2PlatformAdvertisementDetail> platformAdvertisementDetails=new ArrayList<>();
           for (PlatformAdvertisementDetailDto dto :platformAdvertisementDetailDtos){
               H2PlatformAdvertisementDetail h2PlatformAdvertisementDetail=new H2PlatformAdvertisementDetail();
               BeanUtil.copyProperties(dto,h2PlatformAdvertisementDetail);
               h2PlatformAdvertisementDetail.setPid(id);
               platformAdvertisementDetails.add(h2PlatformAdvertisementDetail);
           }
           saveBatch(platformAdvertisementDetails);
        }
    }

    @Override
    public void deletebyPid(Long id) {
        h2PlatformAdvertisementDetailMapper.deletebyPid(id);
    }

    @Override
    public List<PlatformAdvertisementDetailVO> getByPId(Long id) {
        LambdaQueryWrapper<H2PlatformAdvertisementDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(H2PlatformAdvertisementDetail::getPid, id);
        queryWrapper.orderByAsc(H2PlatformAdvertisementDetail::getSortNum);
        List<H2PlatformAdvertisementDetail> list=list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)){
            List<PlatformAdvertisementDetailVO> platformAdvertisementDetailVOS=new ArrayList<>();
            for (H2PlatformAdvertisementDetail h2PlatformAdvertisementDetail:list){
                PlatformAdvertisementDetailVO platformAdvertisementDetailVO=new PlatformAdvertisementDetailVO();
                BeanUtil.copyProperties(h2PlatformAdvertisementDetail,platformAdvertisementDetailVO);
                platformAdvertisementDetailVOS.add(platformAdvertisementDetailVO);
            }
            return platformAdvertisementDetailVOS;
        }
        return null;
    }
}
